﻿using System;
using System.IO;
using System.Management;
using System.Net;
using System.Net.Sockets;

namespace ChangeIPTool
{
    class Program
    {
        /// <summary>
        /// 管理员权限运行
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //1.获取当前的IP地址(IPV4)
            string ip = GetIP();

            if(string.IsNullOrEmpty(ip))
            {
                Console.WriteLine("获取IP失败");
                Console.ReadLine();
            }
            //2.当前IP地址与配置的IP相同 （IPConfig.txt）
            //2.1 将IP地址改为自动获取
            string config_text = File.ReadAllText("./IPConfig.txt");
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            if (config_text.Equals(ip))
            {
                Set_Default_NetWork(moc);
            }
            //3.当前IP地址与配置的IP地址不相同 （IPConfig.txt）
            //3.1 将IP地址改为配置的IP地址
            else
            {
                Set_NetWork(config_text,ip, moc);
            }
            
        }

        private static void Set_Default_NetWork(ManagementObjectCollection moc)
        {
            foreach(ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"])
                    continue;
                mo.InvokeMethod("SetDNSServerSearchOrder", null);
                mo.InvokeMethod("EnableDHCP", null);

                //mo.InvokeMethod("EnableStatic", null);
                //mo.InvokeMethod("SetGateways", null);
                return;
            }
        }

        private static void Set_NetWork(string config_text,string ip, ManagementObjectCollection moc)
        {
            ManagementBaseObject inpar=null;
            foreach(ManagementObject mo in moc)
            {
                if (!(bool)mo["IPEnabled"])
                    continue;
                if (((string[])mo["IPAddress"])[0] == ip)
                {
                    inpar = mo.GetMethodParameters("EnableStatic");
                    //设置ip地址和子网掩码
                    inpar["IPAddress"] = new string[] { config_text };
                    inpar["SubnetMask"] = new string[] { "255.255.255.0" };
                    mo.InvokeMethod("EnableStatic", inpar, null);

                    //网关地址
                    inpar = mo.GetMethodParameters("SetGateways");
                    inpar["DefaultIPGateway"] = new string[] { "192.168.1.1" };
                    mo.InvokeMethod("SetGateways", inpar, null);

                    //    //设置DNS 
                    //    //inpar = mo.GetMethodParameters("SetDNSServerSearchOrder");
                    //    //inpar["DNSServerSearchOrder"] = new string[] { "192.168.0.1" };  
                    //    //outpar = mo.InvokeMethod("SetDNSServerSearchOrder", inpar, null);
                    return;
                }
            }

        }

        private static string GetIP()
        {
            string name = Dns.GetHostName();
            IPAddress[] iPs = Dns.GetHostAddresses(name);

            foreach (var ip in iPs)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
            return null;
        }
    }
}
